|
1
|
|
|
/* eslint-disable jsx-a11y/interactive-supports-focus */ |
|
2
|
|
|
import React from 'react'; |
|
3
|
|
|
import PropTypes from 'prop-types'; |
|
4
|
|
|
import { BrowserRouter as Router, withRouter } from 'react-router-dom'; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* A React component to trigger Router links with a delay |
|
8
|
|
|
*/ |
|
9
|
|
|
export class DelayLink extends React.Component { |
|
10
|
|
|
/** |
|
11
|
|
|
* Constructor. Sets up basic properties and calls parent constructor |
|
12
|
|
|
* @param {*} props |
|
13
|
|
|
*/ |
|
14
|
|
|
constructor(props) { |
|
15
|
|
|
super(props); |
|
16
|
|
|
this.timeout = null; |
|
17
|
|
|
this.handleClick = this.handleClick.bind(this); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Component is about to unmount from the DOM |
|
22
|
|
|
*/ |
|
23
|
|
|
componentWillUnmount() { |
|
24
|
|
|
if (this.timeout) { |
|
25
|
|
|
clearTimeout(this.timeout); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Click (and keyboard) event handler. |
|
31
|
|
|
* Invokes the function specified in the clickAction property. |
|
32
|
|
|
* Then, update browser history (provided by React Router) after a given delay. |
|
33
|
|
|
* |
|
34
|
|
|
* @param {Event} e |
|
35
|
|
|
*/ |
|
36
|
|
|
handleClick(e) { |
|
37
|
|
|
const { |
|
38
|
|
|
to, clickAction, history, |
|
39
|
|
|
} = this.props; |
|
40
|
|
|
let { delay, replace } = this.props; |
|
41
|
|
|
|
|
42
|
|
|
delay = parseInt(delay, 10); |
|
43
|
|
|
replace = !!replace; |
|
44
|
|
|
|
|
45
|
|
|
if (clickAction instanceof Function) { |
|
46
|
|
|
clickAction(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (e.defaultPrevented) { |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
e.preventDefault(); |
|
53
|
|
|
|
|
54
|
|
|
this.timeout = setTimeout(() => { |
|
55
|
|
|
if (replace) { |
|
56
|
|
|
history.push.replace(to); |
|
57
|
|
|
} else { |
|
58
|
|
|
history.push(to); |
|
59
|
|
|
} |
|
60
|
|
|
}, delay); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Render method. Wraps the component's children around a DIV and sets up event handlers |
|
65
|
|
|
*/ |
|
66
|
|
|
render() { |
|
67
|
|
|
const props = Object.assign({}, this.props); |
|
68
|
|
|
const { children } = this.props; |
|
69
|
|
|
delete props.delay; |
|
70
|
|
|
return ( |
|
71
|
|
|
<Router> |
|
72
|
|
|
<div role="link" onClick={this.handleClick} onKeyPress={this.handleClick}>{children}</div> |
|
73
|
|
|
</Router> |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
DelayLink.propTypes = { |
|
79
|
|
|
/** |
|
80
|
|
|
* Milliseconds to wait before registering the click. |
|
81
|
|
|
*/ |
|
82
|
|
|
delay: PropTypes.number, |
|
83
|
|
|
/** |
|
84
|
|
|
* Whether to replace the current URL with the link's or push a new one. |
|
85
|
|
|
*/ |
|
86
|
|
|
replace: PropTypes.bool, |
|
87
|
|
|
/** |
|
88
|
|
|
* The Link's URL |
|
89
|
|
|
*/ |
|
90
|
|
|
to: PropTypes.string, |
|
91
|
|
|
/** |
|
92
|
|
|
* The history object (provided by React Router) |
|
93
|
|
|
*/ |
|
94
|
|
|
history: PropTypes.oneOfType([PropTypes.object]), |
|
95
|
|
|
/** |
|
96
|
|
|
* The Component's child elements (provided by React) |
|
97
|
|
|
*/ |
|
98
|
|
|
children: PropTypes.oneOfType([PropTypes.element]), |
|
99
|
|
|
/** |
|
100
|
|
|
* An optional function to invoke before setting up the timeout |
|
101
|
|
|
*/ |
|
102
|
|
|
clickAction: PropTypes.oneOfType([PropTypes.func]), |
|
103
|
|
|
}; |
|
104
|
|
|
|
|
105
|
|
|
DelayLink.defaultProps = { |
|
106
|
|
|
delay: 0, |
|
107
|
|
|
replace: false, |
|
108
|
|
|
to: '', |
|
109
|
|
|
history: {}, |
|
110
|
|
|
children: null, |
|
111
|
|
|
clickAction: () => {}, |
|
112
|
|
|
}; |
|
113
|
|
|
|
|
114
|
|
|
// Apply the "withRouter" HOC and enable history access |
|
115
|
|
|
export default withRouter(DelayLink); |